﻿using DapperQ;
using MessageQuque.Common.Base.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MessageQuque.Common.Base.Task
{
    public class TaskBase : ConfigurationSectionBase, ITask
    {
        /// <summary>
        /// 处理方法
        /// </summary>
        public virtual void Process()
        { 
            
        }

        /// <summary>
        /// 任务周期
        /// </summary>
        public PeriodType PeriodType { get; set; }

        /// <summary>
        /// 任务执行时间（小时部份）
        /// </summary>
        public int Hour { get; set; }

        /// <summary>
        /// 任务执行时间（分钟部份）
        /// </summary>
        public int Minutes { get; set; }

        protected Dictionary<Type, IDbConnection> Connections { get; set; }
        protected DapperQ<T1> GetDbContext<T1>() where T1 : class, new()
        {
            DapperQ<T1> db = null;

            var type = typeof(T1).GetCustomAttributes(typeof(DapperQ.Mapping.DataSource), true).FirstOrDefault().GetType();
            if (type == null)
                return db;

            if (Connections == null)
            {
                Connections = new Dictionary<Type, IDbConnection>();
            }

            if (!Connections.ContainsKey(type))
            {
                db = new DapperQ<T1>();
                Connections.Add(type, db.Connection);
            }
            else
            {
                db = new DapperQ<T1>(Connections[type]);
            }

            return db;
        }
    }
}
